﻿// -----------------------------------------------------------------------
// Copyright (c) David Kean. All rights reserved.
// -----------------------------------------------------------------------
using System;
using System.Text;
using System.Threading;
using Moq;

namespace Portable
{
    internal class SynchronizationContextFactory
    {
        public static SynchronizationContext Create()
        {
            var mock = CreateMock();

            return mock.Object;
        }

        public static SynchronizationContext OverrideOperationCompleted(Action action)
        {
            var mock = CreateMock();
            mock.Setup(s => s.OperationCompleted())
                .Callback(action);

            return mock.Object;
        }

        public static SynchronizationContext OverrideOperationStarted(Action action)
        {
            var mock = CreateMock();
            mock.Setup(s => s.OperationStarted())
                .Callback(action);

            return mock.Object;
        }

        public static SynchronizationContext OverridePost(Action<SendOrPostCallback, object> action)
        {
            var mock = CreateMock();
            mock.Setup(s => s.Post(It.IsAny<SendOrPostCallback>(), It.IsAny<object>()))
                .Callback(action);

            return mock.Object;
        }

        private static Mock<SynchronizationContext> CreateMock()
        {
            var mock = new Mock<SynchronizationContext>();
            mock.CallBase = true;

            return mock;
        }
    }
}
